package dxcorp.dxadminapi.controller;

import dxcorp.dxadminapi.common.Constants;
import dxcorp.dxadminapi.common.ResultGenerator;
import dxcorp.dxadminapi.common.Result;
import dxcorp.dxadminapi.entity.waferinfo;
import dxcorp.dxadminapi.service.waferinfoService;
import dxcorp.dxadminapi.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping("/typesetup")
public class WaferInfoController {

    @Autowired
    private waferinfoService waferinfoService;

    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR, "参数异常！");
        }
        //查询列表数据
        PageUtil pageUtil = new PageUtil(params);
        return ResultGenerator.genSuccessResult(waferinfoService.getWaferInfoPage(pageUtil));
    }

    @PostMapping("/add")
    public Result save(@RequestBody waferinfo bzsbasic) {

        if (waferinfoService.save(bzsbasic) > 0) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("添加失败");
        }
    }

    @PostMapping(value = "/update")
    public Result update(@RequestBody waferinfo waferinfo) {
        System.out.println(waferinfo);
        if (waferinfoService.update(waferinfo)==null) {
            System.out.println(waferinfo);
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("修改失败");
        }
    }


    @PostMapping("/delete")
    public Result delete(@RequestBody Integer[] ids) {

        if (waferinfoService.deleteBatch(ids) > 0) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        System.out.println("I am here");
        if (!file.isEmpty()) {
            String filename="";
            try {
                /*
                 * 这段代码执行完毕之后，图片上传到了工程的跟路径； 大家自己扩散下思维，如果我们想把图片上传到
                 * d:/files大家是否能实现呢？ 等等;
                 * 这里只是简单一个例子,请自行参考，融入到实际中可能需要大家自己做一些思考，比如： 1、文件路径； 2、文件名；
                 * 3、文件格式; 4、文件大小的限制;
                 */
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(
                                file.getOriginalFilename())));
                System.out.println(file.getName());
                filename = file.getOriginalFilename();
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
            System.out.println("success");
            return filename + "上传成功";


        } else {
            return "上传失败，因为文件是空的.";
        }
    }




}
